<?php
/**
*btc列表
*/
require_once 'common.inc.php';

require_once MCC_ROOT.'/useroperation.inc.php';
$matches=parse_url($_SERVER['HTTP_REFERER']);

$userarray=array('username','realname','tentqq','email','password');

//$url=$matches['scheme'].'://'.$matches['host'].'/';
//!in_array('0',$larr)&&adminmsg('你没有被赋予操作该项的权限！');'7'=>'btchina','8'=>'xunlei',
$warray=array('0'=>'直接输入','1'=>'btzero','2'=>'cngba','3'=>'cnxp','4'=>'ydy','5'=>'fr','6'=>'btcool','9'=>'其它网站');
$sql="select * from ps_website";
$query=$dp->query($sql);
while($result=$dp->fetch_array($query)){
	$warray[]=$result['website'];
}
error_reporting(0);

$sql="select ctime from ps_count order by ctime asc limit 1";
$query=$dp->query($sql);
$times=$dp->fetch_array($query);
$sql="select ctime from ps_count order by ctime desc limit 1";
$query=$dp->query($sql);
$timee=$dp->fetch_array($query);
$starttime=$times['ctime'];
$endtime=$timee['ctime'];
if('list'==$action)
{
	
	$sql="select count(*) from ps_count where dwebsite!='0'";
	$query=$dp->query($sql);
	$count=$dp->fetch_array($query);
	//-----------------------------------
	$yssql="select count(*) as ys from ps_down where dtype='1'";
	$ysquery=$dp->query($yssql);
	$ysys = $dp->result($ysquery,0);
	$infos['original'] = $ysys;
	$tjsql="select count(*) as ys from ps_down where dtype='0'";
	$tjquery=$dp->query($tjsql);
	$tjtj = $dp->result($tjquery,0);
	$infos['digest'] = $tjtj;
	//$infos['opercent'] = 
	//--------------------------------------	
	$iptablesql = "select count(distinct cip) num from ps_count ";
	$iptablequery = $dp->query($iptablesql);
    $iptables=0;
	$ipl = $dp->fetch_array($iptablequery);
	$iptables=$ipl['num'];
/*	
	$downiptalequery = $dp->query("select  count(*) from ps_down group by dip");
    $diptables=0;
	while($dipl = $dp->fetch_array($downiptalequery))
        {
               $diptables++;
		}
*/
    $infos['totalip']=$iptables;
	//$infos['totalip']=$count['count(*)'];//IP访问总数
	$infos['totalpage']=$count['count(*)'];//推送页面显示次数	
	$infos['downs']=$ysys+$tjtj;
	//$infos['downs']=$original+$digest;
	$infos['dpercent']=round($infos['digest']/$infos['downs'],4)*100;//下载推荐种子次数/总下载次数   
	$infos['opercent']=round($infos['original']/$infos['downs'],4)*100;//下载原始种子次数/总下载次数
	$infos['percent']=round($infos['downs']/$infos['totalpage'],4)*100;//总下载次数/总显示次数
	//---------------------------
	$infos['fdpercent']=ceil(($infos['digest']/$infos['downs'])*100)*3;//下载推荐种子次数/总下载次数   
	$infos['fopercent']=ceil(($infos['original']/$infos['downs'])*100)*3;//下载原始种子次数/总下载次数
	$infos['fpercent']=ceil(($infos['downs']/$infos['totalpage'])*100)*3;//总下载次数/总显示次数	
		if($infos['fpercent']>300)
		{
			$infos['fpercent'] = 300;
		}	
	//分别统计各个网站的访问次数
	//echo "<!--";
	$sql="select count(*)num,A.dwebsite,B.id,B.website from ps_count A left join ps_website B on A.dwebsite=B.id where enable='1'  and dwebsite!='0' group by A.dwebsite order by num desc";
	$wsql=$dp->query($sql);
	$abc=array();
	while($wcount=$dp->fetch_array($wsql))$abc[]=$wcount;
	foreach($abc as $key => $list)
	{
		if(!$list['website'])continue;
		$webinfos.=$list['website'].':'.$list[num].'次&nbsp;&nbsp;&nbsp;';
		$sum+=$list[num];
	}
/*	$sql="select count(*)num from ps_count where dwebsite=0 group by dwebsite";
	$query=$dp->query($sql);
	$number=$dp->fetch_array($query);
*/
	$other=($infos[totalpage]-$sum);

	//echo "<br>".$webinfo;
	//echo "-->";
    include_once MCC_ROOT.'/templates/msystem/down.htm';
}
if('search'==$action)
{
	$stimeo=$stime;
	$etimeo=$etime;
	if(!ereg("-",$stimeo))$stimeo=date('Y-m-d',$stimeo);
	if(!ereg("-",$etimeo))$etimeo=date('Y-m-d',$etimeo);
	if(empty($page))
	{
		$stime=GmtToUnix($stime);
		$etime=GmtToUnix($etime);
	}
	if($stime>$etime)
		adminmsg('时间参数错误','down.inc.php?action=list');	
	else
	{	
	//IP来源
		$iplist=array();
		$iptables = array();
		$infos=array();//其他总的统计信息

		//$limit=100;		
		//$page=intval($page)<1?1:intval($page);
		//$start=(intval($page)-1)*$limit;
		
		$where='';
		if($webname!='-1')
			$where=" and dwebsite='$webname'";

	$sql="select count(*) from ps_count where ctime>='$stime' and ctime<='$etime'$where";
	$query=$dp->query($sql);
	$count=$dp->fetch_array($query);
	//-----------------------------------
	$yssql="select count(*) as ys from ps_down where dtime>='$stime' and dtime<='$etime' and dtype='1' $where";
	$ysquery=$dp->query($yssql);
	$ysys = $dp->result($ysquery,0);
	$infos['original'] = $ysys;
	$tjsql="select count(*) as ys from ps_down where dtime>='$stime' and dtime<='$etime' and dtype='0'$where";
	$tjquery=$dp->query($tjsql);
	$tjtj = $dp->result($tjquery,0);
	$infos['digest'] = $tjtj;
	//$infos['opercent'] = 
	//--------------------------------------	
	$iptablesql = "select count(distinct cip) num from ps_count where ctime>='$stime' and ctime<='$etime'$where";
	$iptablequery = $dp->query($iptablesql);
    $iptables=0;
	$ipl = $dp->fetch_array($iptablequery);
	$iptables=$ipl['num'];
    $infos['totalip']=$iptables;
	//$infos['totalip']=$count['count(*)'];//IP访问总数
	$infos['totalpage']=$count['count(*)'];//推送页面显示次数	
	$infos['downs']=$ysys+$tjtj;
	//$infos['downs']=$original+$digest;
	$infos['dpercent']=round($infos['digest']/$infos['downs'],4)*100;//下载推荐种子次数/总下载次数   
	$infos['opercent']=round($infos['original']/$infos['downs'],4)*100;//下载原始种子次数/总下载次数
	$infos['percent']=round($infos['downs']/$infos['totalpage'],4)*100;//总下载次数/总显示次数
	//---------------------------
	$infos['fdpercent']=ceil(($infos['digest']/$infos['downs'])*100)*3;//下载推荐种子次数/总下载次数   
	$infos['fopercent']=ceil(($infos['original']/$infos['downs'])*100)*3;//下载原始种子次数/总下载次数
	$infos['fpercent']=ceil(($infos['downs']/$infos['totalpage'])*100)*3;//总下载次数/总显示次数	
		if($infos['fpercent']>300)
		{
			$infos['fpercent'] = 300;
		}

	//echo "<!--";
	$sql="
	select count(*)num,A.dwebsite,B.id,B.website from ps_count A left join ps_website B on A.dwebsite=B.id where A.ctime>='$stime' and A.ctime<='$etime' and enable='1' group by A.dwebsite order by num desc";
	$wsql=$dp->query($sql);
	$abc=array();
	while($wcount=$dp->fetch_array($wsql))$abc[]=$wcount;
	foreach($abc as $key => $list)
	{
		if(!$list['website'])continue;
		$webinfos.=$list['website'].':'.$list[num].'次&nbsp;&nbsp;&nbsp;';
		$sum+=$list[num];
	}
	$yssql="select count(*) as ys from ps_down where dtype='1'";
	$ysquery=$dp->query($yssql);
	$ysys = $dp->result($ysquery,0);
	$tjsql="select count(*) as ys from ps_down where dtype='0'";
	$tjquery=$dp->query($tjsql);
	$tjtj = $dp->result($tjquery,0);
	//echo "<br>".$webinfo;
	//echo "-->";
	}
	$sql="select count(*)num from ps_count where dwebsite=0 and ctime>='$stime' and ctime<='$etime'group by dwebsite";
	$query=$dp->query($sql);
	$number=$dp->fetch_array($query);

	$other=($infos[totalpage]-$sum)-$number[num];
	include_once MCC_ROOT.'/templates/msystem/down.htm';
}elseif('listall'==$action){
	$sql="select * from ps_website where enable='0'";
	$query=$dp->query($sql);
	while($result=$dp->fetch_array($query))
		$listsite[]=$result;

	$sql="select count(*)num,dwebsite from ps_count group by dwebsite";
	$query=$dp->query($sql);
	while($result=$dp->fetch_array($query))
		$listnum[]=$result;

	for($i=0;$i<count($listsite);$i++){
		for($y=0;$y<count($listnum);$y++){
			if($listsite[$i][id]==$listnum[$y][dwebsite]){
				$listsite[$i][num]=$listnum[$y][num];
				break;
			}else{
				$listsite[$i][num]=0;
			}
		}
	}

	/*
	$sql="select count(*)num,A.dwebsite,B.id,B.website from ps_count A right join ps_website B on A.dwebsite=B.id group by B.id order by B.id";
	$query=$dp->query($sql);
	while($result=$dp->fetch_array($query))
	$listsite[]=$result;*/

	include_once MCC_ROOT.'/templates/msystem/downsite.htm';

}elseif('searchall'==$action){
	$stimeo=$stime;
	$etimeo=$etime;
	if(!ereg("-",$stimeo))$stimeo=date('Y-m-d',$stimeo);
	if(!ereg("-",$etimeo))$etimeo=date('Y-m-d',$etimeo);
	if(empty($page))
	{
		$stime=GmtToUnix($stime);
		$etime=GmtToUnix($etime);
	}
	if($stime>$etime)
		adminmsg('时间参数错误','down.inc.php?action=list');	
	else
	{
	$sql="select * from ps_website where enable='0'";
	$query=$dp->query($sql);
	while($result=$dp->fetch_array($query))
		$listsite[]=$result;

	$sql="select count(*)num,dwebsite from ps_count where ctime>='$stime' and ctime<='$etime' group by dwebsite";
	$query=$dp->query($sql);
	while($result=$dp->fetch_array($query))
		$listnum[]=$result;

	for($i=0;$i<count($listsite);$i++){
		for($y=0;$y<count($listnum);$y++){
			if($listsite[$i][id]==$listnum[$y][dwebsite]){
				$listsite[$i][num]=$listnum[$y][num];
				break;
			}else{
				$listsite[$i][num]=0;
			}
		}
	}
	}
	include_once MCC_ROOT.'/templates/msystem/downsite.htm';

}



function GmtToUnix($GmtDate)
{
 $DateArr = explode(' ',$GmtDate); // 分割GMT日期为 日期 | 时间

 /* 在日期中取得年,月,日 */
 $pDate = split('[/.-]',$DateArr[0]);
 $Year = $pDate[0];
 $Month = $pDate[1];
 $Day = $pDate[2];
 
 /* 在时间中取得时,分,秒 */
 $pTime = split('[:.-]',$DateArr[1]);
 $Hour = $pTime[0];
 $Minute = $pTime[1];
 $Second = $pTime[2];
 
 if($Year == '' || !is_numeric($Year))$Year = 0;
 if($Month == '' || !is_numeric($Month))$Month = 0;
 if($Day == '' || !is_numeric($Day))$Day = 0;
 if($Hour == '' || !is_numeric($Hour))$Hour = 0;
 if($Minute == '' || !is_numeric($Minute))$Minute = 0;
 if($Second == '' || !is_numeric($Second))$Second = 0;
 return mktime($Hour,$Minute,$Second,$Month,$Day,$Year);
}

?>
